﻿Imports System.Data.SqlClient
Imports System.IO
Imports System.Xml
Imports System.Data
Imports Microsoft.Win32
Imports Microsoft.ReportingServices
Imports Microsoft.Reporting.WinForms


Public Class FrmProductos

    Private stock As Integer
    Private total As Decimal
    Private porc As Decimal = 0
    Dim formatotext As clsFormulario
    Dim controller As New Class_RC_Controller.rc_Productos
    Private m_frmCostoProducto As FrmCostoProducto

    Public Property frmCostoProducto() As FrmCostoProducto
        Get
            Return m_frmCostoProducto
        End Get
        Set(ByVal value As FrmCostoProducto)
            m_frmCostoProducto = value
        End Set
    End Property
    Private _codProducto As Integer = 0

    Public Property codProducto() As Integer
        Get
            Return _codProducto
        End Get
        Set(value As Integer)
            _codProducto = value
            txtcodigo.Text = value.ToString().PadLeft(4, "0")
        End Set
    End Property
    Private _TotalCosto As Decimal = 0

    Public Property totalCosto() As Decimal
        Get
            Return _TotalCosto
        End Get
        Set(ByVal valor As Decimal)
            _TotalCosto = valor
            'txtmano.Text = valor.ToString().PadLeft(4, "0")
        End Set
    End Property

    Private Sub CargarDetalles()
        If codProducto > 0 Then

            Dim dt As New DataTable
            dt = controller.get_Productos_Detalles_All(oiml.dbConn, codProducto)
            If Not dt Is Nothing Then
                dgvdetalle.DataSource = dt
            Else
                dgvdetalle.DataSource = ""

            End If

            totales()
            calcularutilidad()

        End If

    End Sub

    Private Sub CargarProducto()
        Try
            If codProducto > 0 Then

                Dim ds As New DataTable
                ds = controller.get_Productos_All(oiml.dbConn, codProducto, "", 0, False)

                If Not ds Is Nothing Then
                    codProducto = ds.Rows(0)("codProducto")
                    txtdescripcion.Text = ds.Rows(0)("descProducto")
                    'txtcosto.Text = ds.Rows(0)("costoMano")
                    txtcantex.Text = ds.Rows(0)("cantExistente")
                    txtcantmin.Text = ds.Rows(0)("cantMin")
                    txtcantmax.Text = ds.Rows(0)("cantMax")
                    cmbmedida.SelectedValue = ds.Rows(0)("codUniMedida")
                    cmbtipo.SelectedValue = ds.Rows(0)("codTipoProducto")
                    txtcosto.Text = ds.Rows(0)("TotalMateriales")
                    txtmano.Text = ds.Rows(0)("costoMano")
                    txtutilidad.Text = ds.Rows(0)("utilidad")
                    cmbporcentage.Text = ds.Rows(0)("PorcUtilidad")
                    txtpreciototal.Text = ds.Rows(0)("precioVenta")
                    lblUsuario.Text = ds.Rows(0)("usuario").ToString
                    lblfechaActualizacion.Text = DateTime.Parse(ds.Rows(0)("fechaActualizacion")).ToShortDateString

                    CargarDetalles()
                    Habilitar()

                    txtcantex.Enabled = False

                    If controller.Control_Productos_Ventas(oiml.dbConn, codProducto) Then
                        btneliminar.Enabled = False
                        btnguardar.Enabled = False
                        btnagregar.Enabled = False
                        btnEliminarPro.Enabled = False

                    End If

                End If

            Else

                txtcantex.Enabled = True
                txtdescripcion.Text = ""
                txtcosto.Text = 0
                txtcantex.Text = 0
                txtcantmin.Text = 0
                txtcantmax.Text = 0
                txtmano.Text = 0
                txtutilidad.Text = 0
                txtpreciototal.Text = 0
                lblUsuario.Text = oiml.Usuario
                lblfechaActualizacion.Text = Date.Now.ToShortDateString
                ControlMaterial()
                txtdescripcion.Focus()
                NuevoProducto()

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub inhabilitar()
        'Datos Producto
        txtcodigo.Enabled = False
        txtdescripcion.Enabled = False
        txtcosto.Enabled = False
        txtprecio.Enabled = False
        txtcantex.Enabled = False
        txtcantmin.Enabled = False
        txtcantmax.Enabled = False
        cmbmedida.Enabled = False
        cmbtipo.Enabled = False
        txtpreciototal.Enabled = False
        txtcodigo.BackColor = System.Drawing.Color.White
        txtdescripcion.BackColor = System.Drawing.Color.White
        txtcosto.BackColor = System.Drawing.Color.White
        txtprecio.BackColor = System.Drawing.Color.White
        txtcantex.BackColor = System.Drawing.Color.White
        txtcantmin.BackColor = System.Drawing.Color.White
        txtcantmax.BackColor = System.Drawing.Color.White
        cmbmedida.BackColor = System.Drawing.Color.White
        cmbtipo.BackColor = System.Drawing.Color.White
        txtpreciototal.BackColor = System.Drawing.Color.White
        'Detalle del  Producto
        cmbmaterial.Enabled = False
        txtcantidad.Enabled = False
        txtprecio.Enabled = False
        txttotal.Enabled = False
        dgvdetalle.Enabled = False
        btnagregar.Enabled = False
        btneliminar.Enabled = False
        btnguardar.Enabled = False
        btncancelar.Enabled = False
        'btnnuevo.Enabled = True
        'btncerrar.Enabled = True
        cmbmaterial.BackColor = System.Drawing.Color.White
        txtcantidad.BackColor = System.Drawing.Color.White
        txtprecio.BackColor = System.Drawing.Color.White
        txttotal.BackColor = System.Drawing.Color.White
        txtmano.Enabled = False
        txtutilidad.Enabled = False
        txtpreciototal.Enabled = False
        cmbporcentage.Enabled = True
        txtmano.BackColor = System.Drawing.Color.White
        txtutilidad.BackColor = System.Drawing.Color.White
        txtpreciototal.BackColor = System.Drawing.Color.White
        cmbporcentage.BackColor = System.Drawing.Color.White
    End Sub

    Private Sub Habilitar()
        txtdescripcion.Enabled = True
        txtcantex.Enabled = True
        txtcantmin.Enabled = True
        txtcantmax.Enabled = True
        cmbmedida.Enabled = True
        cmbtipo.Enabled = True
        'Detalle del  Producto
        cmbmaterial.Enabled = True
        txtcantidad.Enabled = True
        'dgvdetalle.Enabled = True
        btnagregar.Enabled = True
        btneliminar.Enabled = True
        btnguardar.Enabled = True
        btncancelar.Enabled = True
        txtmano.Enabled = True
        'btnnuevo.Enabled = False
        cmbporcentage.Enabled = True
        'btncerrar.Enabled = False

    End Sub

    Private Sub limpiar()
        txtcodigo.Text = ""
        txtdescripcion.Text = ""
        txtcosto.Text = ""
        txtprecio.Text = ""
        txtcantex.Text = ""
        txtcantmin.Text = ""
        txtcantmax.Text = ""
        cmbmedida.Text = ""
        cmbtipo.Text = ""
        txtpreciototal.Text = ""
        txtmano.Text = ""
        txtutilidad.Text = ""
        cmbporcentage.Text = ""
        cmbmaterial.Text = ""
    End Sub

    Private Sub llenarcbomedida()

        Dim dsunimedida As DataTable
        dsunimedida = controller.get_tipo_unidMedida(oiml.dbConn)
        With Me.cmbmedida
            .DataSource = dsunimedida
            .DisplayMember = "DescUnidadMedida"
            .ValueMember = "CodUnidadMedida"
        End With

    End Sub

    Private Sub llenarcbotipo()

        Dim dstipoproducto As DataTable
        dstipoproducto = controller.get_tipo_Productos(oiml.dbConn)
        With Me.cmbtipo
            .DataSource = dstipoproducto
            .DisplayMember = "DescTipoProducto"
            .ValueMember = "CodTipoProducto"
        End With

    End Sub

    Private Sub llenarcbomaterial()

        Dim dsmaterial As DataTable
        dsmaterial = controller.get_Materiales(oiml.dbConn, 0)
        If Not dsmaterial Is Nothing Then
            If dsmaterial.Rows.Count > 0 Then
                cmbmaterial.DataSource = dsmaterial
                cmbmaterial.DisplayMember = "DescMaterial"
                cmbmaterial.ValueMember = "CodMaterial"
            End If
        End If

    End Sub

    Private Sub ArmaMaterial()
        Try

            If cmbmaterial.SelectedValue.ToString() <> "" And cmbmaterial.SelectedValue.ToString() <> "System.Data.DataRowView" Then
                Dim dsmaterial As DataTable
                dsmaterial = controller.get_Materiales_Detalle(oiml.dbConn, cmbmaterial.SelectedValue)
                If Not dsmaterial Is Nothing Then
                    txtcantidad.Text = 1
                    txtprecio.Text = FormatNumber(dsmaterial.Rows(0)("CostoUnidad"), 0, , , TriState.True)
                    CalculoCantidadMaterial()
                Else
                    txtprecio.Text = 0
                End If
            Else
                txtprecio.Text = 0
            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        
    End Sub

    Private Sub txtcantidad_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtcantidad.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            e.Handled = True
            SendKeys.Send("{TAB}")
        End If
        If e.KeyChar.ToString() = "." Then
            e.KeyChar = ","
        End If
    End Sub

    Private Sub txtcantidad_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtcantidad.KeyUp
        'pasamos  los parametros al sub KeyUp que creamos
        Me.KeyUp(Me.txtcantidad, e)
    End Sub

    Private Function ValidarCampos() As Boolean

        Dim controlCampos As Boolean = False

        If Not IsNumeric(txtcantmin.Text) Then
            ErrorProvider.SetError(txtcantmin, "Campo numerico, Costo")
            controlCampos = True
        Else
            ErrorProvider.SetError(txtcantmin, "")
        End If        '

        If Not IsNumeric(txtcantmax.Text) Then
            ErrorProvider.SetError(txtcantmax, "Campo numerico, Costo")
            controlCampos = True
        Else
            ErrorProvider.SetError(txtcantmax, "")
        End If        '

        If Not IsNumeric(txtcantex.Text) Then
            ErrorProvider.SetError(txtcantex, "Campo numerico, Costo")
            controlCampos = True
        Else
            ErrorProvider.SetError(txtcantex, "")
        End If        '


        If txtDescripcion.Text = "" Then
            ErrorProvider.SetError(txtDescripcion, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider.SetError(txtDescripcion, "")
        End If

        If txtcantmin.Text = "" Then
            ErrorProvider.SetError(txtcantmin, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider.SetError(txtcantmin, "")
        End If

        If txtcantmax.Text = "" Then
            ErrorProvider.SetError(txtcantmax, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider.SetError(txtcantmax, "")
        End If

        If txtcantex.Text = "" Then
            ErrorProvider.SetError(txtcantex, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider.SetError(txtcantex, "")
        End If

        If txtmano.Text = 0 Then
            ErrorProvider.SetError(txtmano, "Debe realizar el calculo de costo de mano de Obra")
            controlCampos = True
        Else
            ErrorProvider.SetError(txtmano, "")
        End If

        If txtutilidad.Text = 0 Then
            ErrorProvider.SetError(txtutilidad, "Seleccionar porcentaje de utilidad")
            controlCampos = True
        Else
            ErrorProvider.SetError(txtutilidad, "")
        End If


        If txtpreciototal.Text = 0 Then
            ErrorProvider.SetError(txtpreciototal, "Debe realizar el calculo de costo y agregar % utilidad")
            controlCampos = True
        Else
            ErrorProvider.SetError(txtpreciototal, "")
        End If
        '
        Return controlCampos

    End Function

    Private Function ValidarDetalleCampos() As Boolean

        Dim controlCampos As Boolean = False

        If txtdescripcion.Text = "" Then
            ErrorProvider.SetError(txtdescripcion, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider.SetError(txtdescripcion, "")
        End If

        If Not IsNumeric(txtcantidad.Text) Then
            ErrorProvider.SetError(txtcantidad, "Campo numerico, Cantidad")
            controlCampos = True
        Else
            ErrorProvider.SetError(txtcantidad, "")
        End If        '

        If Not IsNumeric(txtprecio.Text) Then
            ErrorProvider.SetError(txtprecio, "Campo numerico, Precio")
            controlCampos = True
        Else
            ErrorProvider.SetError(txtprecio, "")
        End If        '

        If Not IsNumeric(txttotal.Text) Then
            ErrorProvider.SetError(txttotal, "Campo numerico, Total")
            controlCampos = True
        Else
            ErrorProvider.SetError(txttotal, "")
        End If
        '
        If txtcosto.Text = "" Then
            ErrorProvider.SetError(txtcosto, "Debe agregar un detalle al Producto")
            controlCampos = True
        Else
            ErrorProvider.SetError(txtcosto, "")
        End If

        Return controlCampos

    End Function

    Private Sub btnguardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnguardar.Click
        
        If ValidarCampos() Then
            Exit Sub
        End If

        If codProducto = 0 Then

            codProducto = controller.Insert_Productos(oiml.dbConn, txtdescripcion.Text,
                                                            Decimal.Parse(txtcantex.Text), Decimal.Parse(txtcantmin.Text),
                                                            Decimal.Parse(txtcantmax.Text), Decimal.Parse(cmbmedida.SelectedValue),
                                                            Decimal.Parse(cmbtipo.SelectedValue), Decimal.Parse(txtutilidad.Text),
                                                            Decimal.Parse(txtmano.Text), Decimal.Parse(txtpreciototal.Text),
                                                            Decimal.Parse(txtcosto.Text), Decimal.Parse(porc), oiml.idUsuario)
            If codProducto > 0 Then
                MessageBox.Show("Registro grabado con exito!", "Productos", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Me.DialogResult = Windows.Forms.DialogResult.OK
                Me.Close()

            Else
                MessageBox.Show("Error: " & controller.MessageError.Message, "Productos", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        Else

            If controller.Update_Productos(oiml.dbConn, codProducto, txtdescripcion.Text,
                                                            Decimal.Parse(txtcantex.Text), Decimal.Parse(txtcantmin.Text),
                                                            Decimal.Parse(txtcantmax.Text), Decimal.Parse(cmbmedida.SelectedValue),
                                                            Decimal.Parse(cmbtipo.SelectedValue), Decimal.Parse(txtutilidad.Text),
                                                            Decimal.Parse(txtmano.Text), Decimal.Parse(txtpreciototal.Text),
                                                            Decimal.Parse(txtcosto.Text), Decimal.Parse(porc), oiml.idUsuario) Then

                MessageBox.Show("Registro grabado con exito!", "Productos", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Me.DialogResult = Windows.Forms.DialogResult.OK
                Me.Close()

            Else
                MessageBox.Show("Error: " & controller.MessageError.Message, "Productos", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        End If

        limpiar()
        inhabilitar()

    End Sub

    Private Sub btncerrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Close()
    End Sub

    'Permite que el Textbox solo acepte Numeros
    'Nombre del TextBox que quiere validar para que acepte numeros </param>
    'Pase la letra 'e' como valor en el evento KeyUp</param>

    Private Shadows Sub KeyUp(ByVal TextBoxs As System.Windows.Forms.TextBox, ByVal e As System.Windows.Forms.KeyEventArgs)

        'Teclas que Utilizaremos El cual solo son las numericas
        If e.KeyData = Keys.Space Or e.KeyData = Keys.Decimal Or _
         e.KeyData = Keys.NumPad0 Or e.KeyData = Keys.NumPad1 Or e.KeyData = Keys.NumPad2 Or e.KeyData = Keys.NumPad3 _
           Or e.KeyData = Keys.NumPad4 Or e.KeyData = Keys.NumPad5 Or e.KeyData = Keys.NumPad6 Or _
           e.KeyData = Keys.NumPad7 Or e.KeyData = Keys.NumPad8 Or e.KeyData = Keys.NumPad9 _
           Or e.KeyData = Keys.D0 Or e.KeyData = Keys.D1 Or e.KeyData = Keys.D2 Or e.KeyData = Keys.D3 Or _
           e.KeyData = Keys.D4 Or e.KeyData = Keys.D5 Or e.KeyData = Keys.D6 _
           Or e.KeyData = Keys.D7 Or e.KeyData = Keys.D8 Or e.KeyData = Keys.D9 Or e.KeyData = Keys.Oemcomma _
            Then

            'Programamos la tecla de Retroceso en el textbox
        ElseIf e.KeyData = Keys.Back And TextBoxs.Text.Length > 0 Then
            On Error Resume Next
            'Removemos un caracter mediante se presione la tecla Back
            TextBoxs.Text.Remove(TextBoxs.SelectionStart - 1)

            'si el textbox es menor que un caracter
        ElseIf TextBoxs.Text.Length < 1 Then
            'ponemos el textbox En Blanco
            TextBoxs.Text = String.Empty
            'ponemos la variable    NumerosYaescritos en Blanco

        Else
            'ponemos el textbox enblanco
            TextBoxs.Text = 0 ' String.Empty
            'Hacemos Sonar un Beep para indicarle al usuario que ha presionado un caracter y no un Numero
            'Beep()
            'MsgBox("Introducir solo valores numericos")
        End If

    End Sub

    Private Sub totales()
        Dim i As Integer
        'Dim total, igv, totalp As Single
        Dim total As Integer
        For i = 0 To dgvdetalle.Rows.Count - 1
            total += dgvdetalle.Rows(i).Cells(4).Value
        Next

        Me.txtcosto.Text = total
        'igv = total * 19 / 100
        'txtigv.Text = igv.ToString("n1")
        'totalp = total + igv
        'Me.txtfinal.Text = totalp.ToString("n1")
    End Sub

    Private Sub btneliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btneliminar.Click
        Try
            If dgvdetalle.SelectedRows.Count > 0 Then
                Dim codMaterial As Integer
                codMaterial = dgvdetalle.SelectedRows(0).Cells(0).Value

                If codMaterial > 0 And MessageBox.Show("Desea eliminar el registro seleccionado?", "Productos", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then

                    If controller.Delete_Productos_Detalle(oiml.dbConn, codProducto, codMaterial) Then
                        CargarDetalles()
                        dgvdetalle.Refresh()
                    Else
                        MessageBox.Show("Error: " & controller.MessageError.Message, "Productos", MessageBoxButtons.OK, MessageBoxIcon.Error)

                    End If

                End If

            End If

        Catch ex As Exception
            MsgBox(ex.Message)

        End Try

    End Sub

    Private Sub FrmProductos_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        dgvdetalle.AutoGenerateColumns = False
        llenarcbomaterial()
        llenarcbotipo()
        llenarcbomedida()
        limpiar()
        Habilitar()
        CargarProducto()
        
    End Sub

    Private Sub ControlMaterial()
        If cmbmaterial.Items.Count > 0 Then
            If cmbmaterial.SelectedValue.ToString() <> "" And cmbmaterial.SelectedValue.ToString() <> "System.Data.DataRowView" Then
                ArmaMaterial()
            End If

        End If
    End Sub

    Private Sub cmbmaterial_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbmaterial.SelectedIndexChanged
        
        ControlMaterial()

    End Sub

    Private Sub txtcantidad_LostFocus(sender As Object, e As System.EventArgs) Handles txtcantidad.LostFocus
        
        CalculoCantidadMaterial()

    End Sub

    Private Sub CalculoCantidadMaterial()
        Try
            Dim a, b, d As Single

            If txtcantidad.Text = "" Then
                txtcantidad.Text = 1
            End If

            a = CDec(txtcantidad.Text)

            If Me.txtprecio.Text = "" Then
                Me.txtprecio.Text = 0
            End If

            b = CDec(txtprecio.Text)

            d = a * b

            txttotal.Text = FormatNumber(d, 0, , , TriState.True)

            'f = Val(txtcosto.Text)
            'g = d + f

        Catch ex As Exception
            txttotal.Text = 0

        End Try
    End Sub

    Private Sub btnagregar_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnagregar.Click
        Try
            If txtcantidad.Text = "" And txtprecio.Text = "" And txttotal.Text = "" Then
                MessageBox.Show("Falta llenar datos del detalle", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1)
                Me.txtcantidad.Focus()
            Else

                If ValidarDetalleCampos() Then
                    Exit Sub
                End If

                If codProducto = 0 Then

                    codProducto = controller.Insert_Productos(oiml.dbConn, txtdescripcion.Text,
                                                            Decimal.Parse(txtcantex.Text), Decimal.Parse(txtcantmin.Text),
                                                            Decimal.Parse(txtcantmax.Text), Decimal.Parse(cmbmedida.SelectedValue),
                                                            Decimal.Parse(cmbtipo.SelectedValue), Decimal.Parse(txtutilidad.Text),
                                                            Decimal.Parse(txtmano.Text), Decimal.Parse(txtpreciototal.Text),
                                                            Decimal.Parse(txtcosto.Text), Decimal.Parse(porc), oiml.idUsuario)
                    If codProducto = 0 Then
                        MessageBox.Show("Error: " & controller.MessageError.Message, "Productos", MessageBoxButtons.OK, MessageBoxIcon.Error)
                        Exit Sub
                    End If

                End If

                If codProducto > 0 Then

                    '--------------
                    If controller.Update_Productos(oiml.dbConn, codProducto, txtdescripcion.Text,
                                                            Decimal.Parse(txtcantex.Text), Decimal.Parse(txtcantmin.Text),
                                                            Decimal.Parse(txtcantmax.Text), Decimal.Parse(cmbmedida.SelectedValue),
                                                            Decimal.Parse(cmbtipo.SelectedValue), Decimal.Parse(txtutilidad.Text),
                                                            Decimal.Parse(txtmano.Text), Decimal.Parse(txtpreciototal.Text),
                                                            Decimal.Parse(txtcosto.Text), Decimal.Parse(porc), oiml.idUsuario) Then

                        If controller.Insert_Productos_Detalle(oiml.dbConn, codProducto, cmbmaterial.SelectedValue, txtcantidad.Text, txtprecio.Text) Then
                            CargarDetalles()
                        Else
                            MessageBox.Show("Error: " & controller.MessageError.Message, "Productos", MessageBoxButtons.OK, MessageBoxIcon.Error)

                        End If

                    Else
                        MessageBox.Show("Error: " & controller.MessageError.Message, "Productos", MessageBoxButtons.OK, MessageBoxIcon.Error)

                    End If
                    '--------------                    

                End If

            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        
        End Try

        cmbmaterial.Focus()
        txtcantidad.Text = ""

    End Sub

    Private Sub btnnuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        NuevoProducto()

    End Sub

    Public Sub NuevoProducto()
        Habilitar()
        txtdescripcion.Focus()
        'btnnuevo.Enabled = False
    End Sub

    Private Sub btncancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncancelar.Click
        'inhabilitar()
        'limpiar()
        Me.Close()
    End Sub

    Private Sub cmbporcentage_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbporcentage.SelectedIndexChanged

        calcularutilidad()

    End Sub

    Private Sub calcularutilidad()
        If Not cmbporcentage.SelectedItem Is Nothing Then
            If cmbporcentage.SelectedItem.ToString() <> "" Then
                Dim totalcosto, manodeobra, utilidad, preciototal As Single
                totalcosto = txtcosto.Text
                manodeobra = txtmano.Text
                porc = cmbporcentage.SelectedItem
                utilidad = (totalcosto + manodeobra) * porc / 100
                preciototal = totalcosto + manodeobra + utilidad
                txtutilidad.Text = utilidad
                txtpreciototal.Text = preciototal
            End If
            
        End If

    End Sub

    'eventos tab con enter
    Private Sub txtdescripcion_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtdescripcion.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            e.Handled = True
            SendKeys.Send("{TAB}")
        End If
    End Sub

    Private Sub txtcantex_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtcantex.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            e.Handled = True
            SendKeys.Send("{TAB}")
        End If
    End Sub

    Private Sub txtcantmin_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtcantmin.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            e.Handled = True
            SendKeys.Send("{TAB}")
        End If
    End Sub

    Private Sub txtcantmax_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtcantmax.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            e.Handled = True
            SendKeys.Send("{TAB}")
        End If
    End Sub

    Private Sub cmbmedida_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles cmbmedida.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            e.Handled = True
            SendKeys.Send("{TAB}")
        End If
    End Sub

    Private Sub cmbtipo_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles cmbtipo.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            e.Handled = True
            SendKeys.Send("{TAB}")
        End If
    End Sub

    Private Sub cmbmaterial_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles cmbmaterial.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            e.Handled = True
            SendKeys.Send("{TAB}")
        End If
    End Sub

    Private Sub txtcantex_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtcantex.KeyUp

        If txtcantex.Enabled Then
            Me.KeyUp(Me.txtcantex, e)
        End If


    End Sub

    Private Sub txtcantmin_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtcantmin.KeyUp
        Me.KeyUp(Me.txtcantmin, e)
    End Sub

    Private Sub txtcantmax_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtcantmax.KeyUp
        Me.KeyUp(Me.txtcantmax, e)
    End Sub

    Private Sub btncalc_Click(sender As System.Object, e As System.EventArgs) Handles btncalc.Click

        'FrmCostoProducto.Show()
        'Me.Close()
        Try
            If txtcodigo.Text <> "" Then
                Dim cod As New Integer
                cod = CInt(txtcodigo.Text)

                If frmCostoProducto Is Nothing Then

                    frmCostoProducto = New FrmCostoProducto()
                    frmCostoProducto.codProducto = cod
                    'If frmClientes.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    frmCostoProducto.ShowDialog()
                    txtmano.Text = frmCostoProducto.totalpres
                    'frmCostoProducto.CargarDetalles()
                    frmCostoProducto.Dispose()
                    frmCostoProducto = Nothing
                    'End If

                Else
                    frmCostoProducto.codProducto = cod
                    frmCostoProducto.ShowDialog()
                    txtmano.Text = frmCostoProducto.totalpres
                End If
                'txtmano.Text = totalCosto
            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    
    Private Sub btnEliminarPro_Click(sender As System.Object, e As System.EventArgs) Handles btnEliminarPro.Click
        Try
            If codProducto > 0 Then
                If MessageBox.Show("Desea eliminar el registro seleccionado?", "Productos", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then

                    If controller.Delete_Productos(oiml.dbConn, codProducto) Then
                        Me.Close()

                    Else
                        MessageBox.Show("Error: " & controller.MessageError.Message, "Productos", MessageBoxButtons.OK, MessageBoxIcon.Error)

                    End If

                End If
            End If
            

        Catch ex As Exception
            MsgBox(ex.Message)

        End Try

    End Sub

End Class

